<?php

class Maintenance implements EaSoftModule {

    public $coreURL;
    public $moduleId;
    public $builder;
    public $upload;
    public $windowTittle;
    public $tittle;
    public $aditionalScript;
    public $validations = array();
    public $table = "";
    public $formName = "form1";
    public $db;
    public $widgets;
    public $includePath;

    public function init($db) {
        $this->widgets = new Widget();
        $this->builder = new InputBuilder();
        $this->widgets->db = $db;
        $this->builder->db = $db;

        $this->builder->rootPath = RELATIVE_PATH;
        $this->builder->loadFields($this->table);
        $this->widgets->rootPath = RELATIVE_PATH . "js_v2/";
        $this->widgets->registerTable($this->table);
    }

    function scriptValidations() {
        $rules = "";
        $index = 0;
        foreach ($this->validations as $v) {
            $rules.= ( $index == 0) ? "" : ",";
            $rules.="'" . $v . "'";
            $index++;
        }
        return '  var form=document.' . $this->formName . ';
			  if(form){
				 rsv= new RSV({
							formID: form,
							errorFieldClass: "error",
							rules: [' . $rules . '],
							handlerParam: form,
							displayType : "display-html",
							errorHTMLItemBullet : "&#8212; "
						});
			  }';
    }

    function loadDynValidations() {
        $where = sprintf("TABLE_NAME='%s' AND STATUS=1", $this->table);
        $this->db = DbManager::getMe();
        $rs = $this->db->query_select("cllg_tablefield", $where);
        foreach ($rs as $r) {
            //datatype validations
            $field = "," . $r['NAME'] . ",";
            $rule = "";
            switch ($r['TYPE']) {
                case '1':
                    $rule = "is_alpha" . $field . $r['CAPTION'] . " debe contener letras o numeros!";  //string
                    break;
                case '2':
                    $rule = "digits_only" . $field . $r['CAPTION'] . " es de tipo entero!";  //integer
                    break;
                case '3':
                    $rule = "decimalNumber" . $field . $r['CAPTION'] . " es de tipo decimal!";
                    ;  //string
                    break;
                case '5':
                    $rule = "valid_email" . $field . $r['CAPTION'] . " es invalido!";
                    ;  //string
                    break;
            }
            if ($rule != "")
                $this->validations[] = $rule;

            //validation required
            if ($r['REQUIRED'] == '1') {
                $rule = "required" . $field . $r['CAPTION'] . " es requerido!";
                $this->validations[] = $rule;
            }
        }
    }

    public function getCssFiles() {
        ?>
        <link href="<?php echo RELATIVE_PATH; ?>js_v2/jquery-ui-1.8.7.custom/development-bundle/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
        <link href="<?php echo RELATIVE_PATH; ?>css/maintenance_v2.css" rel="stylesheet" type="text/css" />
        <link href="<?php echo RELATIVE_PATH; ?>css/grid/css/ui.jqgrid.css" rel="stylesheet" type="text/css" media="screen" />
        <link href="<?php echo RELATIVE_PATH; ?>js_v2/multiselect/css/jquery.multiselect.css"  rel="stylesheet" type="text/css" media="screen" />
        <link href="<?php echo RELATIVE_PATH; ?>css/tablereportes.css"  rel="stylesheet" type="text/css" media="screen" />
        <?php
    }

    public function getJsFiles() {
        ?>      <script type="text/javascript"  src="<?php echo RELATIVE_PATH; ?>js_v2/jquery-ui-1.8.7.custom/js/jquery-ui-1.8.7.custom.min.js"></script>
        <script type="text/javascript"  src="<?php echo RELATIVE_PATH; ?>js_v2/form/jquery.form.js"></script>
        <script type="text/javascript"  src="<?php echo RELATIVE_PATH; ?>js_v2/form/jquery.validation.js"></script>
        <script type="text/javascript" src="<?php echo RELATIVE_PATH; ?>js_v2/grid/grid.locale-es.js" ></script>
        <script type="text/javascript" src="<?php echo RELATIVE_PATH; ?>js_v2/grid/jquery.jqgrid.min.js" ></script>
        <script type="text/javascript"  src="<?php echo RELATIVE_PATH; ?>js_v2/multiselect/jquery.multiselect.min.js"  > </script>
        <script type="text/javascript"  src="<?php echo RELATIVE_PATH; ?>js_v2/splitter/splitter.js"  > </script>
        <script type="text/javascript"  src="<?php echo RELATIVE_PATH; ?>js_v2/maintenancev2.js"  > </script>
        <!-- INCLUYENDO WIDGETS -->
        <?php
        if ($this->widgets != null) {
            $this->widgets->includes();
            $this->widgets->activateWidgets();
        }
        ?>
        <!-- FIN WIDGETS -->
        <?PHP
    }

    public function main() {
        
    }

    public function getWindowTitle() {
        return $this->windowTittle;
    }

    public function getScript() {
        echo $this->loadDynValidations();
        echo $this->scriptValidations();
    }

}
?>